package com.girant.logger.entity.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.dromara.easyes.annotation.*;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.annotation.rely.IdType;


//业务日志实体类（映射ES索引 business-logs-*）
@Data
@IndexName(value = "business-logs-*") // 匹配索引名
@JsonIgnoreProperties(ignoreUnknown = true) // 忽略未定义字段
public class LogDocumentDO {
    //ES默认主键id
    @IndexId(type = IdType.NONE)
    private String id;

    //操作用户ID
    @JsonProperty("operation_user") // JSON序列化时的字段名
    @IndexField(value = "operation_user", fieldType = FieldType.KEYWORD) // 与索引字段名一致
    private String operationUser;

    //操作用户账号
    @JsonProperty("operation_username") // 对应索引字段名
    @MultiIndexField(
            mainIndexField = @IndexField(
                    value = "operation_username",
                    fieldType = FieldType.TEXT,
                    analyzer = "ik_max_word",
                    searchAnalyzer = "ik_smart"
            ),
            otherIndexFields = {
                    @InnerIndexField(suffix = "keyword", fieldType = FieldType.KEYWORD)
            })
    private String operationUsername;

    // 用户昵称
    @JsonProperty("operation_nickname") // 对应索引字段名
    @MultiIndexField(
            mainIndexField = @IndexField(
                    value = "operation_nickname",
                    fieldType = FieldType.TEXT,
                    analyzer = "ik_max_word",
                    searchAnalyzer = "ik_smart"
            ),
            otherIndexFields = {
                    @InnerIndexField(suffix = "keyword", fieldType = FieldType.KEYWORD)
            })
    private String operationNickname;

    //服务器实例名称
    @JsonProperty("server_name")
    @IndexField(value = "server_name", fieldType = FieldType.KEYWORD)
    private String serverName;

    // 服务器实例名称标签
    private String serverNameLabel;

    //来源IP
    @JsonProperty("source_ip")
    @IndexField(value = "source_ip", fieldType = FieldType.IP) // 与索引类型一致
    private String sourceIp;

    //客户端类型
    @JsonProperty("client_type")
    @IndexField(value = "client_type", fieldType = FieldType.KEYWORD)
    private String clientType;

    //内容分类
    @JsonProperty("content_category")
    @IndexField(value = "content_category", fieldType = FieldType.KEYWORD)
    private String category;

    //客户端浏览器
    @JsonProperty("client_browser")
    @IndexField(value = "client_browser", fieldType = FieldType.KEYWORD)
    private String clientBrowser;

    //客户端版本
    @JsonProperty("client_version")
    @IndexField(value = "client_version", fieldType = FieldType.KEYWORD)
    private String clientVersion;

    // 操作时间
    @JsonProperty("log_timestamp") // 对应索引字段名（原实体类中变量名与索引不一致，需修正）
    @IndexField(
            value = "log_timestamp",
            fieldType = FieldType.DATE,
            dateFormat = "yyyy-MM-dd HH:mm:ss" // 与索引格式完全一致
    )
    private String operationTime; // 变量名可保持业务含义，通过@JsonProperty映射索引字段名

    @JsonProperty("title")
    @MultiIndexField(
            mainIndexField = @IndexField(
                    value = "title",
                    fieldType = FieldType.TEXT,
                    analyzer = "ik_max_word",
                    searchAnalyzer = "ik_smart"
            ),
            otherIndexFields = {
                    @InnerIndexField(suffix = "keyword", fieldType = FieldType.KEYWORD)
            }
    )
    private String title;

    //内容
    @JsonProperty("log_message")
    @MultiIndexField(
            mainIndexField = @IndexField(
                    value = "log_message",
                    fieldType = FieldType.TEXT,
                    analyzer = "ik_max_word",
                    searchAnalyzer = "ik_smart"
            ),
            otherIndexFields = {
                    @InnerIndexField(suffix = "keyword", fieldType = FieldType.KEYWORD)
            }
    )
    private String content; // 变量名保持业务含义，通过@JsonProperty映射索引字段名

    //追踪ID
    @JsonProperty("trace_id")
    @IndexField(value = "trace_id", fieldType = FieldType.KEYWORD)
    private String traceId;

    //跨度ID
    @JsonProperty("span_id")
    @IndexField(value = "span_id", fieldType = FieldType.KEYWORD)
    private String spanId;

    // 异常堆栈信息
    @JsonProperty("stack_trace")
    @MultiIndexField(
            mainIndexField = @IndexField(
                    value = "stack_trace",
                    fieldType = FieldType.TEXT,
                    analyzer = "ik_max_word",
                    searchAnalyzer = "ik_smart"
            ),
            otherIndexFields = {
                    @InnerIndexField(suffix = "keyword", fieldType = FieldType.KEYWORD)
            })
    private String stackTrace;

    //日志级别
    @JsonProperty("log_level")
    @IndexField(value = "log_level", fieldType = FieldType.KEYWORD)
    private String logLevel;

    //日志类型（后续建立字典）
    @JsonProperty("log_type")
    @IndexField(value = "log_type", fieldType = FieldType.KEYWORD)
    private String logType;

    private String clientTypeLabel;

    private String categoryLabel;

    @JsonProperty("request_params")
    @IndexField(value = "request_params", fieldType = FieldType.KEYWORD)
    private String requestParams;
}